<template>
  <List ref="qmpList" v-model:finished="finished" :reset-fun="resetData" :load="load" :refresh="load">
    <Item
      v-for="(item, i) in list"
      :key="i"
      :item-data="item"
      @followUp="toFollowUp(item)"
      @detail="toDetail(item)"
      :dictData="dictData"
    />
  </List>
</template>
<script lang="ts">
  import { ref, reactive, toRefs, defineComponent, watch } from 'vue'
  import { useRouter } from 'vue-router'
  import List from '@/components/List/index.vue'
  import Item from './Item.vue'
  import { getList } from '@/apis/storeUpgrade/index'
  import { getDictByPatchType } from '@/apis/dataDict'

  export default defineComponent({
    name: 'StoreUpgraderList',
    components: { List, Item },
    props: {
      searchValue: {
        type: String,
        default: ''
      },
      type: {
        type: String,
        default: ''
      }
    },
    setup(props) {
      const qmpList = ref()
      const $router = useRouter()
      const datas = reactive({
        list: [],
        finished: false,
        dictData: {}
      })
      const load = async (page: number, rows: number) => {
        const filtersRaw: object[] = []
        if (props.searchValue) {
          filtersRaw.push({
            id: 'marketName',
            property: 'marketName',
            operator: 'like',
            value: props.searchValue
          })
        }
        if (props.type === 'tobe_allocate') {
          // 待跟进查询
          const list = ['A', 'D']
          filtersRaw.push({
            id: 'maintenanceStage',
            property: 'maintenanceStage',
            value: `[${list.join(',')}]`,
            operator: 'in'
          })
        }
        const paramsData = {
          page,
          rows,
          sort: 'createTime',
          order: 'desc',
          filtersRaw,
          paramsRaw: {},
          sortersRaw: [],
          pageFlag: true
        }
        return getList(paramsData).then((res: any) => {
          const resData = res.data
          const list = resData.records
          if (page === 1) {
            datas.list = list
          } else {
            datas.list = datas.list.concat(list)
          }
          if (resData.total <= datas.list.length) {
            datas.finished = true
          }
        })
      }
      const resetData = () => {
        datas.list = []
      }
      const resetPageData = () => {
        qmpList.value.resetPageData()
      }

      // 获取数据字典
      const getDictData = async () => {
        const dictTypeCodes = [
          'CTM_MARKET_BUID_FOLLOW_UP_PROGRESS',
          'TOBE_MARKET_TYPE',
          'CTM_MARKET_BUID_WEEK_MAINTENANCE_PROGRESS'
        ]
        datas.dictData = await getDictByPatchType({ dictTypeCodes })
      }

      getDictData()

      const toFollowUp = (row: any) => {
        $router.push({
          path: '/storeUpgrade/edit',
          query: {
            id: row.id
          }
        })
      }

      const toDetail = (row: any) => {
        $router.push({
          path: '/storeUpgrade/detail',
          query: {
            id: row.id,
            marketId: row.marketId
          }
        })
      }

      watch(
        () => props.searchValue,
        () => {
          resetPageData()
        }
      )

      return {
        ...toRefs(datas),
        load,
        resetData,
        qmpList,
        resetPageData,
        toFollowUp,
        toDetail
      }
    }
  })
</script>
<style lang="less" scoped></style>
